package com.framework.modules.base.service.impl;

import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.base.dao.UserPaymentDao;
import com.framework.modules.base.entity.UserPaymentEntity;
import com.framework.modules.base.service.UserPaymentService;


@Service("userPaymentService")
public class UserPaymentServiceImpl extends ServiceImpl<UserPaymentDao, UserPaymentEntity> implements UserPaymentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        EntityWrapper wrapper=new EntityWrapper<UserPaymentEntity>();
        if(params.containsKey("uid")){
            wrapper.eq("uid",params.get("uid"));
        }
        Page<UserPaymentEntity> page = this.selectPage(
                new Query<UserPaymentEntity>(params).getPage(),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<Map<String,Object>> getUserPaymentList(Map<String, Object> params) {
        List<String> listMonthDate=getLast3Months();
        List<Map<String,Object>> list=new ArrayList<>(listMonthDate.size());
        for(String monthDate:listMonthDate){
            Map<String,Object> result=new HashMap<>();
            params.put("monthDate",monthDate);
            List<UserPaymentEntity> userPaymentEntities=this.baseMapper.selectUserPaymentByDate(params);
            result.put("monthDate",monthDate);
            result.put("paymentList",userPaymentEntities);
            list.add(result);
        }
        return list;
    }

    /**
     * 获取最近三个月的月份
     * @return
     */
    public List<String> getLast3Months() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        Date now=new Date();
        List<String> listMonth=new ArrayList<>();
        for(int i=0;i<3;i++){
            Calendar c = Calendar.getInstance();
            c.setTime(now);
            c.add(Calendar.MONTH, -i);
            Date m = c.getTime();
            listMonth.add(sdf.format(m));
        }
        return listMonth;
    }

}
